package com.isnotlocus.gulimail.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.isnotlocus.gulimail.product.service.CategoryBrandRelationService;
import com.isnotlocus.gulimail.product.valid.AddGroup;
import com.isnotlocus.gulimail.product.valid.UpdateGroup;
import com.isnotlocus.gulimail.product.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.isnotlocus.gulimail.product.entity.BrandEntity;
import com.isnotlocus.gulimail.product.service.BrandService;
import com.isnotlocus.common.utils.PageUtils;
import com.isnotlocus.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author IsnotLocus
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 17:00:57
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * BindingResult 绑定一下错误消息的
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")  原来是@valid 注解
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand /*, BindingResult result*/) {

//        if(result.hasErrors()){
//            result.getFieldErrors().forEach((item)->{
//                String message=item.getDefaultMessage();
//                String field=item.getField();
//                map.put(field,message);
//            });
//          return R.error(400,"提交的数据不合法").put("data",map);
//        }else{
        brandService.save(brand);
        return R.ok();


    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        if (brand.getName() != null) {
            boolean b = categoryBrandRelationService.updateFromBranUpdate(brand.getBrandId(), brand.getName());
            if (b) {
                System.out.println("修改成功1！！！");
            }
        }

        return R.ok();
    }


    @RequestMapping("/update/status")
    // @RequiresPermissions("product:brand:update")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
